package com.outstanding.his.check.facade;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.outstanding.his.check.domain.BloodSugar;
import com.outstanding.his.check.domain.CP;
import com.outstanding.his.check.domain.Check;
import com.outstanding.his.check.domain.Insulin;
import com.outstanding.his.check.service.ICheckService;
import com.outstanding.his.patient.domain.Patient;
import com.outstanding.his.patient.service.IPatientService;

@Controller
@SessionAttributes("patient")
public class CheckController {

	private ICheckService checkService;
	private IPatientService patientService;

	public void setCheckService(ICheckService checkService) {
		this.checkService = checkService;
	}

	public void setPatientService(IPatientService patientService) {
		this.patientService = patientService;
	}

	@RequestMapping("/check/submit")
	public String checkSubmit(@ModelAttribute("patient") Patient patient,
			@ModelAttribute("check") Check check,
			@ModelAttribute("bloodSugar") BloodSugar bloodSugar,
			@ModelAttribute("cp") CP cp,
			@ModelAttribute("insulin") Insulin insulin) {
		if (patient.getCheck() != null) {
			check.setCheckId(patient.getCheck().getCheckId());
		}
		check.setBloodSugar(bloodSugar);
		check.setCp(cp);
		check.setInsulin(insulin);
		System.out.println(insulin.getEmptyInsulin());
		patient.setCheck(check);
		check = checkService.submitCheck(check);
		
		patientService.updatePatient(patient);
		return "redirect:/check/view.do?patientId=" + patient.getPatientId();

	}

	@RequestMapping("/check/view.do")
	public String checkView(@RequestParam("patientId") Integer patientId,
			Model model) {
		Patient patient = patientService.getPatient(patientId);
		model.addAttribute("patient", patient);
		return "/check/view";
	}

	@RequestMapping(value = "/check/edit.do", method = RequestMethod.GET)
	public String checkEdit(@RequestParam("patientId") Integer patientId,
			Model model) {
		Patient patient = patientService.getPatient(patientId);
		model.addAttribute("patient", patient);
		return "/check/edit";
	}

}